package com.gray.blog.controller.sys;

import com.gray.blog.arg.sys.AddSysGeeksArg;
import com.gray.blog.arg.sys.GetPageGeeksArg;
import com.gray.blog.arg.sys.UpdateGeeksArg;
import com.gray.blog.common.annotation.IpCheckTrigger;
import com.gray.blog.common.controller.BaseController;
import com.gray.blog.common.enums.ErrorCodeEnum;
import com.gray.blog.common.result.ModelResult;
import com.gray.blog.common.util.BeanUtil;
import com.gray.blog.result.sys.GeeksDetailResult;
import com.gray.blog.result.sys.GetPageGeeksResult;
import com.gray.blog.service.front.FrontGeeksService;
import com.gray.blog.vo.sys.AddSysGeeksVO;
import com.gray.blog.vo.sys.GetPageGeeksVO;
import com.gray.blog.vo.sys.UpdateGeeksInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created  By zhoux 2020/04/24
 **/
@Slf4j
@RestController
@RequestMapping(value = "/sys/front/geeks")
@Api(value = "系统极客内容", description = "系统极客内容相关接口")
@CrossOrigin
public class SysGeeksController extends BaseController {

    @Autowired
    private FrontGeeksService frontGeeksService;

    @RequestMapping(value = "/", method = RequestMethod.POST)
    @IpCheckTrigger
    @ApiOperation(value = "分页获取极客信息")
    @RequiresRoles(logical = Logical.OR, value = {ADMIN})
    public ModelResult<GetPageGeeksResult> getPageGeeks(@RequestBody GetPageGeeksArg arg) {
        if (arg.isWrongParam()) {
            log.warn("SysGeeksController.getPageGeeks param error arg:{}", arg);
            return ModelResult.newError(ErrorCodeEnum.PARAMS_ERROR);
        }
        GetPageGeeksVO vo = BeanUtil.copy(arg, GetPageGeeksVO.class);
        return frontGeeksService.getPageGeeks(vo);
    }

    @RequestMapping(value = "/{id}/", method = RequestMethod.GET)
    @IpCheckTrigger
    @ApiOperation(value = "分页获取极客信息")
    @RequiresRoles(logical = Logical.OR, value = {ADMIN})
    public ModelResult<GeeksDetailResult> getSysGeek(@PathVariable("id") String id) {
        return frontGeeksService.getSysGame(id);
    }

    @RequestMapping(value = "/add/", method = RequestMethod.POST)
    @IpCheckTrigger
    @ApiOperation(value = "新增极客信息")
    @RequiresRoles(logical = Logical.OR, value = {ADMIN})
    public ModelResult addSysGeeks(@RequestBody @Valid AddSysGeeksArg arg) {
        AddSysGeeksVO vo = BeanUtil.copy(arg, AddSysGeeksVO.class);
        return frontGeeksService.addSysGeeks(vo);
    }

    @RequestMapping(value = "/", method = RequestMethod.PUT)
    @IpCheckTrigger
    @ApiOperation(value = "更新极客数据")
    @RequiresRoles(logical = Logical.OR, value = {ADMIN})
    public ModelResult updateGeeks(@RequestBody @Valid UpdateGeeksArg arg) {
        if (arg.isWrongParam()) {
            log.warn("SysGeeksController.updateGeeks param error arg:{}");
            return ModelResult.newError(ErrorCodeEnum.PARAMS_ERROR);
        }
        UpdateGeeksInfoVO vo = BeanUtil.copy(arg, UpdateGeeksInfoVO.class);
        return frontGeeksService.updateSysGeeks(vo);
    }

}
